<?php
/**	
*	FILENAME: 		/server_app/ldap_search.php
*	DESCRIPTION:	This backend script produces a JSON response representing the LDAP search results for a given search query.
*	AUTHOR:			Casey Burkhardt
*	VERSION:		1.0.0
*	LAST MODIFIED:	12/31/2009
**/

require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/ldap.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/libraries/functions.php");

global $SORT_FIELD;
$SORT_FIELD = "name";

/**	
*	DESCRIPTION:	Comparison function used to sort the custom array of LDAP search results.
**/
function compareTo($data1, $data2) {
	global $SORT_FIELD;
	
	if ($data1[$SORT_FIELD] == $data2[$SORT_FIELD]) {
		return 0;
	} elseif ($data1[$SORT_FIELD] < $data2[$SORT_FIELD]) {
		return -1;
	} else {
		return 1;
	}	
}

enforceSSL();

$query = $_POST["query"];

// Check the validity of the search query.
if (strlen($query) == 0) {
	$response["response_type"] = "error";
	$response["error_desc"] = "No query provided.";
	echo(json_encode($response));
	die();
} else {
	// Run the LDAP search for the provided query.
	$searchResults = ldapSearch($query);
	
	// Check if the search failed, or had no results.
	if ($searchResults === FALSE) {
		$response["response_type"] = "error";
		$response["error_desc"] = "Unable to complete directory search.";
		echo(json_encode($response));
		die();
	} else if (empty($searchResults)) {
		$response["response_type"] = "error";
		$response["error_desc"] = "No results found.";
		echo(json_encode($response));
		die();		
	} else {
		$formattedResults = array();
		// Iterate through the search results and create a nicely formatted array to hold the resulting search data.
		for ($i = 0; $i < $searchResults["count"]; $i++) {
			$thisResult = array();
			$thisResult["user_id"] = $searchResults[$i]["uid"][0];
			$thisResult["name"] = $searchResults[$i]["cn"][0];
			// Iterate through the "ou" fields to determine the account type.
			for ($j = 0; $j < $searchResults[$i]["ou"]["count"]; $j++) {
				if (strpos($searchResults[$i]["ou"][$j], "Student") !== FALSE) {
					$thisResult["result_type"] = "Student";
					break;
				} elseif (strpos($searchResults[$i]["ou"][$j], "Faculty") !== FALSE) {
					$thisResult["result_type"] = "Faculty";
					break;
				} elseif (strpos($searchResults[$i]["ou"][$j], "Staff") !== FALSE) {
					$thisResult["result_type"] = "Staff";
					break;
				} elseif (strpos($searchResults[$i]["ou"][$j], "Alumni") !== FALSE) {
					$thisResult["result_type"] = "Alumni";
					break;
				}
			}
			
			// Some LDAP records don't store the account type in an "ou" field like they're supposed to.
			// If an account type wasn't determined, look in the "dn" field for that information.  Otherwise, it will just be "Unknown".
			if (strlen($thisResult["result_type"]) == 0) {
				if (strpos($searchResults[$i]["dn"], "Student") !== FALSE) {
					$thisResult["result_type"] = "Student";
				} elseif (strpos($searchResults[$i]["dn"], "Faculty") !== FALSE) {
					$thisResult["result_type"] = "Faculty";
				} elseif (strpos($searchResults[$i]["dn"], "Staff") !== FALSE) {
					$thisResult["result_type"] = "Staff";
				} elseif (strpos($searchResults[$i]["dn"], "Alumni") !== FALSE) {
					$thisResult["result_type"] = "Alumni";			
				} else {
					$thisResult["result_type"] = "Unknown";
				}
			}
			// Add this result to our final results array.
			$formattedResults[] = $thisResult;
		}
		
		// Now that the results are nicely formatted, let's sort them in alphabetical order by name.
		usort($formattedResults, "compareTo");
		
		// Return the JSON response containing the search result data.
		$response["response_type"] = "success";
		$response["search_results"] = $formattedResults;
		echo(json_encode($response));
		die();	
	}
}
?>